package com.sparkflow.mysqlbinlog.clickhousesync.config.binlog;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
import com.sparkflow.mysqlbinlog.clickhousesync.config.properties.MysqlBinlogProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Binlog客户端配置类
 * 配置MySQL binlog连接客户端
 *
 * @author Mr_xiaoZH
 * @date 2025-07-23
 */
@Configuration
public class BinlogClientConfig {

    @Autowired
    private MysqlBinlogProperties mysqlBinlogProperties;

    @Bean
    public BinaryLogClient binaryLogClient() {
        BinaryLogClient client = new BinaryLogClient(this.mysqlBinlogProperties.getHost(), this.mysqlBinlogProperties.getPort(), this.mysqlBinlogProperties.getUsername(), this.mysqlBinlogProperties.getPassword());
        client.setServerId(this.mysqlBinlogProperties.getServerId());
        // 配置事件反序列化器
        EventDeserializer eventDeserializer = new EventDeserializer();
        eventDeserializer.setCompatibilityMode(
                EventDeserializer.CompatibilityMode.DATE_AND_TIME_AS_LONG,
                EventDeserializer.CompatibilityMode.CHAR_AND_BINARY_AS_BYTE_ARRAY
        );
        client.setEventDeserializer(eventDeserializer);
        return client;
    }
}
